﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Aribaccio
{
    /// <summary>
    /// 命令基类[自定义]
    /// </summary>
    public class CommandBase : ICommand
    {
        private Action<object?> _Method;
        private Action<object?, Action<object?>> _Action;
        private readonly Func<bool> _CanExecuteMethod;
        /// <summary>
        /// Handler事件【实现】
        /// </summary>
        public event EventHandler? CanExecuteChanged;

        /// <summary>
        /// 带参构造函数
        /// </summary>
        /// <param name="action"></param>
        /// <param name="method"></param>
        /// <param name="canExecuteMethod"></param>
        public CommandBase(Action<object?, Action<object?>> action, Action<object?> method, Func<bool> canExecuteMethod)
        {
            _Method = method;
            _Action = action;
            _CanExecuteMethod = canExecuteMethod;
        }

        public bool CanExecute(object? parameter)
        {
            if (_CanExecuteMethod != null)
            {
                return _CanExecuteMethod();
            }
            return true;
        }

        public void Execute(object? parameter)
        {
            if (_Action != null)
            {
                _Action(parameter, _Method);
            }
        }
    }
}
